package com.future.biz;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.future.dao.ThirdFillBlankDAO;
import com.future.dao.TownsDao;
import com.future.dbutils.TransactionUtils;
import com.future.model.po.ThirdFillBlankModel;
import com.future.model.po.UserModel;
import com.future.model.vo.TownsServicesVO;

/**
 * 第三部分 乡镇（街道）和社区（行政村）调查
 * 对乡镇（街道）和社区（行政村）调查页面增查改
 * @author petter
 * Jan 11, 2014
 */
public class TownsServices {
	private static Logger logger = LogManager.getLogger(TownsServices.class);
	private ThirdFillBlankDAO  thirdFillBlankDao = null;
	private TownsDao townsDao =  null;
	/**
	 *查乡镇（街道）和社区（行政村）调查页面 判断题
	 */
	public Map<List<TownsServicesVO>,ThirdFillBlankModel> showServices(UserModel userModel) {
		thirdFillBlankDao = new ThirdFillBlankDAO();
		townsDao = new TownsDao();
		Map<List<TownsServicesVO>,ThirdFillBlankModel> map = 
			new HashMap<List<TownsServicesVO>, ThirdFillBlankModel>();
		List<TownsServicesVO> tsvos = townsDao.findServicesByUserId(userModel);
		ThirdFillBlankModel blankModel = thirdFillBlankDao.findByUserid(userModel);
		map.put(tsvos, blankModel);
		return map;
	}
	
	/**
	 * 修改乡镇（街道）和社区（行政村）调查判断
	 */
	public boolean updateServices(List<TownsServicesVO> servicesVOs,ThirdFillBlankModel fillBlankModel){
		boolean flag = false;
		boolean temp = false;

		thirdFillBlankDao = new ThirdFillBlankDAO();
		townsDao = new TownsDao();
		boolean isExist = thirdFillBlankDao.isOrExist(fillBlankModel);
		boolean isFirst = townsDao.isOrExist((TownsServicesVO)(servicesVOs.get(0)));
		TransactionUtils.startTransaction(); // 开启事务
		
		ThirdFillBlankDAO thirdFillBlank = new ThirdFillBlankDAO(TransactionUtils.getConnection());
		if(isExist){
			temp = thirdFillBlank.updateThirdFillBlank(fillBlankModel);
		} else {
			try {
				temp = thirdFillBlank.saveThirdFillBlank(fillBlankModel);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		TownsDao townDao = new TownsDao(TransactionUtils.getConnection());
		if (isFirst) {
			flag = townDao.updateServicesById(servicesVOs);
		}else {
			flag = townDao.saveServicesAnswer(servicesVOs);
		}
		
		TransactionUtils.commitTransaction(); // 提交事务
		if (flag&&temp) {
			return true;
		}else {
			return false;
		}
	}
	
	
	
}
